<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/commons/global.jsp" %>
<!DOCTYPE html>
<html>
<head>
<%@ include file="/commons/basejs.jsp" %>
<meta http-equiv="X-UA-Compatible" content="edge" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户管理</title>
    <script type="text/javascript">
    var dataGrid;
    var url;
    var resourceTree;
    $(function() {
        dataGrid = $('#dataGrid').datagrid({
            url : '${path}/role/queryByList.do',
            fit : true,
            striped : true,
            rownumbers : true,
            pagination : true,
            singleSelect : true,
            //selectOnCheck:false,
            idField : 'id',
            sortName : 'create_time',
            sortOrder : 'desc',
            pageSize : 10,
            pageList : [ 10, 20, 30, 40],
            loadMsg:'玩命加载中...',
            columns : [ [ 
			{
			    width : '80',
			    title : 'id',
			    field : 'id',
			    hidden	:true
			},{
                width : '80',
                title : '名称',
                field : 'role_name',
                sortable : true
            }, {
                width : '80',
                title : '编码',
                field : 'role_code',
                sortable : true
            },{
                width : '80',
                title : '状态',
                field : 'status',
                formatter:function(value, row, index){
                	switch (value) {
                    case '0':
                        return '正常';
                    case '1':
                        return '删除';
                    }
                },
              	styler:function(value,row,index){
              		switch (value) {
                    case '0':
                        return 'color:green;';
                    case '1':
                        return 'color:red;';
                    }
              	}
            },{
                width : '150',
                title : '创建时间',
                field : 'create_time',
                sortable : true
            }, {
                width : '150',
                title : '修改时间',
                field : 'update_time',
                sortable : true
            }] ],
            toolbar : '#toolbar'
        });
        //查询资源树
        resourceTree = $('#resource_tree').tree({
        	url : '${base}/resource/findAllResourceTree.do',
            parentField : 'pid',
            lines : true,
            animate: true,
            checkbox : true,
            cascadeCheck : false,
            onLoadSuccess : function(node, data) {
            	
            }
        })
    });
    
    function searchFun() {
        dataGrid.datagrid('load', $.serializeObject($('#searchForm')));
    }
    function addFun() {
        $('#dlg_role').dialog('open').dialog('setTitle','新增');
        $('#fm_role').form('clear');
        url = '${base}/role/add.do';
    }
    
    function editFun() {
        var row = $('#dataGrid').datagrid('getSelected');
        if (row){
            $('#dlg_role').dialog('open').dialog('setTitle','编辑');
            $('#fm_role').form('load',row);
            url = '${base}/role/update.do?id='+row.id;
        }
    }
    function saveRole(){
    	if($('#fm_role').form('validate')){
    		$.post(url, $('#fm_role').serialize(), function (data) {
    			var result=eval("("+data+")");
                if (result.code!='0') {
                	$.messager.show({
                        title: 'Error',
                        msg: result.errorMsg
                    });
                }else {
                	$('#dlg_role').dialog('close');        // close the dialog
                    $('#dataGrid').datagrid('reload');    // reload the user data
                }                   
            });
    	}
    }
    function deleteFun() {
        var row = $('#dataGrid').datagrid('getSelected');
        if (row){
            $.messager.confirm('Confirm','删除可能会引起数据错乱,确定要删除吗?',function(r){
                if (r){
                    $.post('${base}/role/delete.do',{id:row.id},function(result){
                        if (result.code=="0"){
                            $('#dataGrid').datagrid('reload');    // reload the user data
                        } else {
                            $.messager.show({    // show error message
                                title: 'Error',
                                msg: result.errorMsg
                            });
                        }
                    },'json');
                }
            });
        }
        
    }
    //授权
    var fm_roleId=''
    function grantAuth(){
    	var row = $('#dataGrid').datagrid('getSelected');
        if (row){
            $('#dlg_key_win').dialog('open').dialog('setTitle','授权');
            progressLoad();
            fm_roleId=row.id;//赋值roleid
            //去掉全部选中
            uncheckAll();
            //加载该角色权限
            $.post('${base}/resource/findRoleResourceListId.do',{
            	roleId:row.id
            },function(result){
            	var ids;
            	if(result != ''){
            		ids = $.stringToList(result + '');
            	}
            	if (ids!=null&&ids.length > 0) {
                    for ( var i = 0; i < ids.length; i++) {
                        if (resourceTree.tree('find', ids[i])) {
                            resourceTree.tree('check', resourceTree.tree('find', ids[i]).target);
                        }
                    }
                }
            },'json');
            progressClose();
        }
    }
    //全部不选中
    function uncheckAll() {
        var nodes = resourceTree.tree('getChecked');
        if (nodes && nodes.length > 0) {
            for ( var i = 0; i < nodes.length; i++) {
                resourceTree.tree('uncheck', nodes[i].target);
            }
        }
    }
    //保存角色资源权限
    function saveAuth(){
    	//获得全部选中节点的Id
    	var nodes = resourceTree.tree('getChecked');
    	var ids = new Array();
    	if(nodes!=null && nodes.length > 0){
	    	for(var i=0;i<nodes.length;i++){
	    		ids[i]=nodes[i].id;
	    	}
    	}
    	$.post('${base}/resource/saveAuth.do',{
    		'resourceIds':ids,'roleId':fm_roleId
    	},function(result){
    		if (result.code=="0"){
    			$('#dlg_key_win').dialog('close');
    			$.messager.alert('提示',result.errorMsg);
            } else {
                $.messager.show({    // show error message
                    title: 'Error',
                    msg: result.errorMsg
                });
            }
    	},'json');
    }
    </script>

</head>
<body class="easyui-layout" data-options="fit:true,border:false">
    <div data-options="region:'north',border:false" style="height: 45px; overflow: hidden;background-color: #fff">
    	<form id="searchForm">
    	<div style="margin-top: 10px;margin-bottom: 10px; margin-left: 10px;">  
          <label>角色名称:</label>  
          <input name="role_name" class ="easyui-textbox" />  
          <label>角色编码:</label>  
          <input name="role_code" class="easyui-textbox" />   
           <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search'" 
           	onclick="searchFun();">Search</a>
       </div>
       </form>
    </div>
    <div data-options="region:'center',border:false" >
        <table id="dataGrid" data-options="fit:true,border:false"></table>
    </div>
    <div id="toolbar" style="display: none;">
    	<c:if test="${fn:contains(roleAuth,'/role/add.do')}">
    	<a onclick="addFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'">添加</a>
    	</c:if>
    	<c:if test="${fn:contains(roleAuth,'/role/update.do')}">
    	<a onclick="editFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-edit'">编辑</a>
    	</c:if>
    	<c:if test="${fn:contains(roleAuth,'/role/delete.do')}">
    	<a onclick="deleteFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-remove'">删除</a>
    	</c:if>
    	<c:if test="${fn:contains(roleAuth,'/role/saveAuth.do')}">
    	<a onclick="grantAuth();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-key'">授权</a>
    	</c:if>
    </div>
    <!-- 弹窗 -->
    <div id="dlg_role" class="easyui-dialog"  style="padding:10px 20px;width: 400px;height: 250px;"
            closed="true" buttons="#dlg-buttons_role">
        <div class="ftitle">详细信息</div>
        <form id="fm_role" method="post" novalidate>
            <div class="fitem">
                <label>角色名称:</label>
                <input name="role_name" class="easyui-textbox" required="true">
            </div>
            <div class="fitem">
                <label>角色编码:</label>
                <input name="role_code" class="easyui-textbox" required="true">
            </div>
        </form>
    </div>
    <div id="dlg-buttons_role">
        <a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="saveRole()" style="width:90px">保存</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg_role').dialog('close')" style="width:90px">取消</a>
    </div>
    <!-- 授权弹窗 -->
    <div id="dlg_key_win" class="easyui-dialog"  style="padding:10px 20px;width: 300px;height: 350px;"
            closed="true" buttons="#dlg-buttons_key_win">
    	<ul id="resource_tree"></ul>
    </div>
    <div id="dlg-buttons_key_win">
        <a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="saveAuth()" style="width:90px">保存</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg_key_win').dialog('close')" style="width:90px">取消</a>
    </div>
</html>